package com.gongpingjialoan.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v4.app.FragmentManager;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.gongpingjialoan.R;
import com.gongpingjialoan.activity.tool.WebViewActivity;
import com.gongpingjialoan.data.NewCarActivityData;
import com.gongpingjialoan.widget.autoviewpagers.LoopPagerAdapter;
import com.gongpingjialoan.widget.autoviewpagers.RollPagerView;

import java.util.List;


public class NewCarLooperAdapter extends LoopPagerAdapter {

    private List<NewCarActivityData.BannersBean> mBannerList;

    private Context mContext;

    private FragmentManager fragmentManager;


    public NewCarLooperAdapter(RollPagerView viewPager, List<NewCarActivityData.BannersBean> banners, Context mContext, FragmentManager fragmentManager) {
        super(viewPager);
        mBannerList = banners;
        this.mContext = mContext;
        this.fragmentManager = fragmentManager;
    }

    public void setBannerList(List<NewCarActivityData.BannersBean> banners){
        mBannerList = banners;
    }


    @Override
    public View getView(ViewGroup container, final int position) {
        ImageView view = new ImageView(container.getContext());
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                gotoWeb(position);
            }
        });
        view.setScaleType(ImageView.ScaleType.CENTER_CROP);
        view.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        Glide.with(mContext).load(mBannerList.get(position).getImage_url()).placeholder(R.drawable.trans).error(R.drawable.trans).into(view);
        return view;
    }

    private void gotoWeb(int position) {
        if(mBannerList != null && position <mBannerList.size()){
            String url = mBannerList.get(position).getUrl();
            if(TextUtils.isEmpty(url))  return;
            Intent intent = new Intent(mContext, WebViewActivity.class);
            intent.putExtra("url", url);
            intent.putExtra("content", "webViewTitle");
            mContext.startActivity(intent);
        }
    }


    @Override
    protected int getRealCount() {
        return mBannerList.size();
    }
}
